{% extends "forums/base_forum.html" %}

{% block content %}
    {% if forum.threads.all %}
        <table class="table">
            <thead>
            <tr>
                <td width="500px">
                    Title
                </td>
                <td>
                    Started by
                </td>
                <td>
                    Date created
                </td>
                <td>
                    Latest post
                </td>
                <td>
                    Posts
                </td>
                {% if forum.competition.creator == request.user %}
                    <td>

                    </td>
                {% endif %}
            </tr>
            </thead>
            <tbody>
            {% for thread in thread_list_sorted %}
                <tr>
                    <td>
                        {% if thread.pinned_date %}
                            <i class="pinned-thread-icon glyphicon glyphicon-pushpin" data-thread-pk="{{ thread.pk }}"></i>
                        {% endif %}

                        <a href="{% url 'forum_thread_detail' forum_pk=forum.pk thread_pk=thread.pk %}">{{ thread.title }}</a>
                    </td>
                    <td>{{ thread.started_by }}</td>
                    <td>{{ thread.date_created|date:"M d, Y" }}</td>
                    <td>{{ thread.last_post_date|timesince }}</td>
                    {# date:"g:iA M d" #}
                    <td>{{ thread.posts.count }}</td>
                    {% if forum.competition.creator == request.user or request.user in forum.competition.admins.all %}
                        <td>
                            <a href="{% url "forum_thread_pin" thread_pk=thread.pk %}"><i class="pin-button glyphicon glyphicon-pushpin" data-thread-pk="{{ thread.pk }}"></i></a>
                            <i class="remove-button glyphicon glyphicon-remove" data-thread-pk="{{ thread.pk }}"></i>
                        </td>
                    {% endif %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
    {% else %}
        <i>No topics started yet, <a href="{% url 'forum_new_thread' forum_pk=forum.pk %}">start one now</a>!</i>
    {% endif %}

    <script>
        function redirect_post(url) {
            var form = document.createElement('form');
            document.body.appendChild(form);
            form.method = 'post';
            form.action = url;
            $(form).append("{% csrf_token %}")
            form.submit();
        }

        $(document).ready(function () {
            $('.remove-button').click(function (event) {
                var thread_pk = event.target.dataset.threadPk
                if (confirm("Are you sure you want to delete this?")) {
                    redirect_post("/forums/{{ forum.pk }}/" + thread_pk + "/delete/")
                }
            })
        })
    </script>
{% endblock %}
